package com.gy.paidao.main;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.google.gson.reflect.TypeToken;
import com.gy.paidao.R;
import com.gy.paidao.bean.BaseBean;
import com.gy.paidao.main.adapter.NotificationAdapter;
import com.gy.paidao.main.bean.ActivityBean;
import com.gy.paidao.main.bean.NotifyBean;
import com.gy.paidao.util.AnimUtil;
import com.gy.paidao.util.LogUtil;
import com.gy.paidao.util.SPHelper;
import com.gy.paidao.util.Urls;
import com.gy.paidao.view.SwipeMenu;
import com.gy.paidao.view.SwipeMenuCreator;
import com.gy.paidao.view.SwipeMenuItem;
import com.gy.paidao.view.SwipeMenuListView;
import com.jky.struct2.http.core.AjaxParams;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * @author GY
 * @date 16/3/2
 * @Description:
 */
public class MainNotificationFragment extends BaseFragment implements AdapterView.OnItemClickListener{

    private static int DELAYED_TIME = 3 * 60 * 1000;
    private View mRoot;
    private List<NotifyBean> mDatas, requestDatas;
    private NotificationAdapter mAdapter;
    private SwipeMenuListView mListView;
    private String userLoginID;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRoot == null) {
            mRoot = inflater.inflate(R.layout.fg_main_notification, null);
            init();
            initView();
            showLoadDialog();
            dorequestNotify();
        }
        ViewGroup parent = (ViewGroup) mRoot.getParent();
        if (parent != null) {
            parent.removeView(mRoot);
        }
        return mRoot;
    }

    @Override
    protected void init() {
        super.init();
        mDatas = new ArrayList<>();
        mAdapter = new NotificationAdapter(mContext, mDatas);
        userLoginID = SPHelper.getInstance(getActivity()).getStringData("userLoginID", null);
        handler.postDelayed(runnable,DELAYED_TIME);
    }

    @Override
    protected void initView() {
        super.initView();
        mListView = (SwipeMenuListView) mRoot.findViewById(R.id.fg_main_list_listview);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem openItem = new SwipeMenuItem(
                        mContext.getApplicationContext());
                openItem.setBackground(new ColorDrawable(Color.parseColor("#FF0000")));
                openItem.setWidth(dp2px(90));
                openItem.setTitle("Delete");
                openItem.setTitleSize(18);
                openItem.setTitleColor(Color.WHITE);
                menu.addMenuItem(openItem);
            }
        };
        mListView.setMenuCreator(creator);

        mListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                LogUtil.e(position + "  " + index);
                switch (index) {
                    case 0:
                        dorequestDelete(mDatas.get(position - 1).id+"");
                        mDatas.remove(position - 1);
                        mAdapter.notifyDataSetInvalidated();
                        break;
                }
                return false;
            }
        });

    }


    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        LogUtil.e("onOptionsItemSelected------");
        return super.onOptionsItemSelected(item);
    }

    private void dorequestNotify() {
        params = new AjaxParams();
        httpRequest.addHeader("Cookie", "userLoginID=" + userLoginID);
        httpRequest.get(Urls.url_notification, params, callBack, 0);
    }

    private void dorequestDelete(String id){
        showLoadDialog();
        params = new AjaxParams();
        params.put("notificationId",id+"");
        httpRequest.addHeader("Cookie", "userLoginID=" + userLoginID);
        httpRequest.get(Urls.url_notify_delete, params, callBack, 1);
    }

    @Override
    protected void handlerBean(int requestCode, BaseBean baseBean) {
        super.handlerBean(requestCode, baseBean);
        dismissLoading();
        if (baseBean.success) {
            switch (requestCode) {
                case 0:
                    String data = String.valueOf(baseBean.data);
                    LogUtil.e(data+"");
                    Type type = new TypeToken<List<NotifyBean>>() {
                    }.getType();
                    requestDatas = gson.fromJson(data, type);
                    mDatas.clear();
                    mDatas.addAll(requestDatas);
                    mAdapter.notifyDataSetChanged();
                    break;
                case 1:

                    break;
            }
        } else {
            if (!TextUtils.isEmpty(baseBean.errMsg)) {
                showToast(baseBean.errMsg);
            }
        }
    }

    private Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
           dorequestNotify();
           handler.postDelayed(runnable,DELAYED_TIME);
        }
    };


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        ActivityBean bean = new ActivityBean();
        NotifyBean notifyBean = mDatas.get(i - 1);
        bean = gson.fromJson(gson.toJson(notifyBean),ActivityBean.class);
        bean.id = notifyBean.activityId;
        Intent intent = new Intent(mContext,BabbleDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("activity",bean);
        intent.putExtras(bundle);
        startActivity(intent);
        AnimUtil.pushLeftInAndOut(getActivity());
    }
}
